//
//  Service+Location.m
//  ali
//
//  Created by fengyn on 16/1/8.
//  Copyright © 2016年 lexingsoft. All rights reserved.
//

#import "Service+Location.h"

@implementation ServiceManager(Location)
///根据父级地区编号查所辖城市列表
+ (void)getAllCityWithParentCode:(NSString *)parentCode success:(void(^)(NSDictionary *data)) success failure:(void(^)(NSDictionary *data)) failure
{
    NSString *path = @"/common/location/jurisdictionOfCity";
    NSDictionary *parameterDict=@{@"parentCode":parentCode,
                                  };
    [NetworkClient getRequest:path parameters:parameterDict NeedPrompt:YES success:^(id dataObject) {
        success(dataObject);
    } failure:^(id resultDict) {
        failure(resultDict);
    }];
}
///获得所有省份直辖市
+ (void)getAllProvinceSuccess:(void(^)(NSDictionary *data)) success failure:(void(^)(NSDictionary *data))failure
{
    NSString * path = @"/common/location/province";
    NSDictionary * dic = nil;
    [NetworkClient getRequest:path parameters:dic NeedPrompt:YES success:^(id dataObject) {
        success(dataObject);
    } failure:^(id dataObject) {
        failure(dataObject);
    }];
}
///从本地数据文件中查西安市所有城区
+ (NSArray *)getAllCityOfXiAn
{
    static NSDictionary *xianDic=nil;
    if (xianDic) {
        return xianDic[@"childs"];
    }
    NSArray *allProvince=[self handleJsonAddress];
    NSString *shanxiCode=@"610000";
    NSString *xianCode=@"610100";
    NSDictionary *shanxiDic=nil;
    for (NSDictionary *dic in allProvince) {
        if ([dic[@"locationCode"] isEqualToString:shanxiCode]) {
            shanxiDic=dic;
            break;
        }
    }
    for (NSDictionary *dic in shanxiDic[@"childs"]) {
        if ([dic[@"locationCode"] isEqualToString:xianCode]) {
            xianDic=dic;
            break;
        }
    }
    return xianDic[@"childs"];
}
///读取本地地区数据文件
+ (NSArray *)handleJsonAddress{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"address_area" ofType:@"json"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSDictionary *dataDic=[NSString jsonObjectWithData:data];
    NSArray *resultArray=dataDic[@"result"];
    return resultArray;
}
@end
